package com.company.poker.channel;

import akka.actor.*;
import akka.camel.Camel;
import akka.camel.CamelExtension;
import com.company.poker.channel.decoder.ByteFrameDecoder;
import com.company.poker.channel.decoder.ByteMessageDecoder;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.netty.ServerPipelineFactory;
import org.apache.camel.impl.DefaultCamelContext;
import org.apache.camel.impl.SimpleRegistry;
import org.apache.camel.spi.Registry;
import org.jboss.netty.handler.codec.string.StringEncoder;
import org.jboss.netty.util.CharsetUtil;


/**
 * Description of class.
 * Created 4/11/13 12:39 PM
 *
 * @author andrey.rodin@playtech.com
 */

public class CamelServer  {
    public static void main(String[] args) throws Exception {
        final ChannelContext context = new ChannelContext(ActorSystem.create("poker"));
        ActorSystem system = context.getActorSystem();
        Camel camel = CamelExtension.get(system);

        DefaultCamelContext camelContext = camel.context();
        SimpleRegistry simpleRegistry = new SimpleRegistry();
        simpleRegistry.put("myDecoder", new ByteFrameDecoder());
        simpleRegistry.put("myDecoder1", new ByteMessageDecoder());
        simpleRegistry.put("myEncoder", new StringEncoder(CharsetUtil.UTF_8));
        camelContext.setRegistry(simpleRegistry);
//        camelContext.addRoutes(new EndpointRouter(context));
        Registry registry = camelContext.getRegistry();
        ServerPipelineFactory serverPipelineFactory = new CamelPipelineFactory();


//        ActorRef endpoint = system.actorOf(new Props(new UntypedActorFactory() {
//            @Override
//            public Actor create() {
//                return new MyEndpoint(context);
//            }
//        }).withDispatcher("default-dispatcher"),"MyEndpoint");

        final CamelProcessor proc= new CamelProcessor(context.getTable());

        RouteBuilder routeBuilder = new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("netty:tcp://localhost:1234?allowDefaultCodec=false&decoders=#myDecoder,myDecoder1&encoders=#myEncoder&orderedThreadPoolExecutor=false")
                        .process(proc);
            }
        };
        routeBuilder.addRoutesToCamelContext(camelContext);

    }


}
